﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace QuickBrowser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void go()
        {
            string inputStr = comboBox1.Text;
            bool isUrl = false;
            if(inputStr.StartsWith("http://") 
                || inputStr.StartsWith("https://") )
            {
                isUrl = true;
            }
            int periodIndex = inputStr.LastIndexOf('.');
            if (periodIndex == inputStr.Length - 3
                || periodIndex == inputStr.Length - 4)
            {
                isUrl = true;
            }
            if(!isUrl)
            {
                string googleStr = "http://www.google.co.kr/search?q=";
                // 삼성 두산 한국 시리즈

                string[] words = inputStr.Split(new Char[] {' '});

                comboBox1.Text = googleStr + String.Join("+", words);

                //comboBox1.Text = googleStr + inputStr.Replace(' ','+');
            }
            Navigate(comboBox1.Text, false);

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Navigate(comboBox1.Text, false);
        }
               
        public void Navigate(string urlString, bool newWindow)
        {
            /**
             
             SELECT  (nums.num + nums_1.num * 10 + nums_2.num * 100)
              AS Expr1
                FROM     nums CROSS JOIN
               nums AS nums_1 CROSS JOIN
               nums AS nums_2
                ORDER BY Expr1
             
             */


            //txtUrl.Text = comboBox1.Text;
            webBrowser1.Navigate(urlString, newWindow);
            SqlCeConnection conn = null;

            try
            {
                conn = new SqlCeConnection("Data Source = Database1.sdf;");
                conn.Open();

                SqlCeCommand cmd = conn.CreateCommand();
                cmd.CommandText = "INSERT INTO History (Url) VALUES  ('" + urlString + "')";

                cmd.ExecuteNonQuery();
                /*
                cmd.CommandText = "Select Count(*) from History";
                Int32 count = (Int32)cmd.ExecuteScalar();
                MessageBox.Show("Count = " + count.ToString());
                */

            }
            catch (Exception e) {
                //Console.WriteLine(e.Message);
                MessageBox.Show(e.Message);
            }
            finally
            {
                conn.Close();
            }
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            SqlCeConnection conn = null;

            try
            {
                conn = new SqlCeConnection("Data Source = Database1.sdf;");
                conn.Open();

                SqlCeCommand command = new SqlCeCommand("Select Distinct Url From History;", conn);
                SqlCeDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    //Console.WriteLine("{0}\t{1}", reader.GetInt32(0), reader.GetString(1));
                    //comboBox1.Items.Add(reader.GetString(0));
                    comboBox1.Items.Add(reader["Url"]);
                }
            }
            catch (Exception e1)
            {
                //Console.WriteLine(e.Message);
                MessageBox.Show(e1.Message);
            }
            finally
            {
                conn.Close();
            }

            //string[] weekDays = { "www.naver.com", "www.daum.net", "www.google.com", "www.yahoo.co.kr" };
            //comboBox1.Items.AddRange(weekDays);
        }

        private void comboBox1_TextUpdate(object sender, EventArgs e)
        {

        }

        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                go();
            }
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            toolStripStatusLabel1.Text = "접속중...";
            toolStripProgressBar1.Maximum = 100;
            toolStripProgressBar1.Value = 100;
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            toolStripStatusLabel1.Text = "준비";
            toolStripProgressBar1.Value = 0;

        }
    }
}
